package com.czkj.hwz.decorate.Activity;

import android.graphics.Color;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.czkj.hwz.decorate.IMP.HttpDoImp;
import com.czkj.hwz.decorate.R;
import com.czkj.hwz.decorate.base.HttpUrl;
import com.czkj.hwz.decorate.bean.Enterprise;
import com.czkj.hwz.decorate.bean.Goods;
import com.czkj.hwz.decorate.bean.MyTab;
import com.czkj.hwz.decorate.bean.Sort;
import com.czkj.hwz.decorate.control.GoodsGet;
import com.czkj.hwz.decorate.control.GoodsSortGet;
import com.czkj.hwz.decorate.control.IndexGet;
import com.joanzapata.android.BaseAdapterHelper;
import com.joanzapata.android.QuickAdapter;

import org.xutils.x;

import java.util.ArrayList;
import java.util.HashMap;

public class ClassifyActivity2 extends AppCompatActivity{
    private static final String TAG = "ClassifyActivity2";
    private LinearLayout ll_h;
    private LinearLayout ll;
    private IndexGet indexGet;
    private GoodsSortGet goodsSortGet;
    private ArrayList<Enterprise> mData;
    MyTab mMyTab_v,mMyTab_h;
    GoodsGet mGoodsSet;
    private HttpDoImp httpDoImp;
    private HttpDoImp goodSortHttpDoImp;
    private ArrayList<Goods> mGoods;
    GridView gv_qb;
    private ArrayList<Sort> sorts;
    private TextView tv_qb;//全部商品textview
    private int enterpriceId;//运营商ID
    private int sortId;//产品Id
    private QuickAdapter<Goods> mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_classify2);
        initView();      
        sortId=-1000;//设置为-1000,方便后面选择全部商品
        tv_qb.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                tv_qb.setTextColor(ClassifyActivity2.this.getResources().getColor(R.color.yellow));
                mMyTab_v.changeColor(-1);
                mGoodsSet=new GoodsGet(ClassifyActivity2.this,httpDoImp);
                HashMap<String,String> reqParams=new HashMap<>();
                reqParams.put("enterpriseid",String.valueOf(enterpriceId));
                mGoodsSet.getList(reqParams);
                sortId=-1000;

            }
        });


        initData();
        getDataFromNetWork();


    }

    @Override
    protected void onResume() {
        super.onResume();

    }

    @Override
    protected void onStart() {
        super.onStart();

    }

    private void initView() {
        ll_h= (LinearLayout) findViewById(R.id.ll_h);
        ll= (LinearLayout) findViewById(R.id.ll);
        gv_qb= (GridView) findViewById(R.id.gv_qb);
        tv_qb= (TextView) findViewById(R.id.tv_qb);
    }
    private void getDataFromNetWork() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                indexGet=new IndexGet(ClassifyActivity2.this,httpDoImp);
                indexGet.getIndex(null);
                goodsSortGet=new GoodsSortGet(ClassifyActivity2.this,goodSortHttpDoImp);
                goodsSortGet.getGoodsSort();
            }
        }).start();
    }
    private void initData() {
        goodSortHttpDoImp=new HttpDoImp() {
            @Override
            public void onSuccess(String flag, Object... objects) {
                switch (flag) {
                    case "GoodsSortGet":
                        sorts= (ArrayList<Sort>) objects[1];
                        mMyTab_v=new MyTab(ClassifyActivity2.this,LinearLayout.VERTICAL) {
                            @Override
                            public void onItemClickListener(int poistion) {
                                int id=sorts.get(poistion).getId();
                                sortId=id;
                                mGoodsSet=new GoodsGet(ClassifyActivity2.this,httpDoImp);
                                HashMap<String,String> reqParams=new HashMap<>();
                                reqParams.put("enterpriseid",String.valueOf(enterpriceId));
                                reqParams.put("classid",String.valueOf(sortId));
                                mGoodsSet.getList(reqParams);
                            }
                        };
                        mMyTab_v.setData(sorts);
                        ll.addView(mMyTab_v.getView());
                        mMyTab_v.setText_Qb(tv_qb);
                        break;
                }
            }

            @Override
            public void onError(String errinfo) {

            }
        };
        httpDoImp=new HttpDoImp() {
            @Override
            public void onSuccess(String flag, Object... objects) {
                switch (flag) {
                    case "indexGet":
                        mData= (ArrayList<Enterprise>) objects[2];
                        enterpriceId=mData.get(0).getId();
                        mGoodsSet=new GoodsGet(ClassifyActivity2.this,httpDoImp);
                        HashMap<String,String> reqParams=new HashMap<>();
                        reqParams.put("enterpriseid",String.valueOf(enterpriceId));
                        mGoodsSet.getList(reqParams);

                        mMyTab_h=new MyTab(ClassifyActivity2.this,LinearLayout.HORIZONTAL) {
                            @Override
                            public void onItemClickListener(int poistion) {
                                int id=mData.get(poistion).getId();
                                enterpriceId=id;
                                mGoodsSet=new GoodsGet(ClassifyActivity2.this,httpDoImp);
                                HashMap<String,String> reqParams=new HashMap<>();
                                reqParams.put("enterpriseid",String.valueOf(id));
                                reqParams.put("classid",String.valueOf(sortId));
                                mGoodsSet.getList(reqParams);

                            }
                        };


                        mMyTab_h.setData(mData);
                        ll_h.addView(mMyTab_h.getView());
                        if (mMyTab_h!=null) {
                            mMyTab_h.changeColor(0);
                        }
                        break;
                    case "getList":
                        mGoods= (ArrayList<Goods>) objects[1];
                        mAdapter=new QuickAdapter<Goods>(ClassifyActivity2.this,R.layout.grideview_goods_item,mGoods) {
                            @Override
                            protected void convert(BaseAdapterHelper helper, Goods item) {
                                helper.setText(R.id.tv_title,item.getName());
                                ImageView img=helper.getView(R.id.img);
                                x.image().bind(img, HttpUrl.BASE_URL+item.getSmallImg());
                            }

                        };
                        gv_qb.setAdapter(mAdapter);


                        break;
                }
            }

            @Override
            public void onError(String errinfo) {

            }
        };
    }

}
